$.extend(jQuery.validator.messages, {
    required: "必选字段",
	remote: "请修正该字段",
	email: "请输入正确格式的电子邮件",
	url: "请输入合法的网址",
	date: "请输入合法的日期",
	dateISO: "请输入合法的日期 (ISO).",
	number: "请输入合法的数字",
	digits: "只能输入整数",
	creditcard: "请输入合法的信用卡号",
	equalTo: "请再次输入相同的值",
	accept: "请输入拥有合法后缀名的字符串",
	maxlength: jQuery.format("请输入一个长度最多是 {0} 的字符串"),
	minlength: jQuery.format("请输入一个长度最少是 {0} 的字符串"),
	rangelength: jQuery.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
	range: jQuery.format("请输入一个介于 {0} 和 {1} 之间的值"),
	max: jQuery.format("请输入一个最大为 {0} 的值"),
	min: jQuery.format("请输入一个最小为 {0} 的值")
});
$.extend({
	coordinate: function(evt){
		var d = {}, x, y;
		if( self.innerHeight ) {
			d.pageYOffset = self.pageYOffset;
			d.pageXOffset = self.pageXOffset;
			d.innerHeight = self.innerHeight;
			d.innerWidth = self.innerWidth;
		} else if( document.documentElement &&
			document.documentElement.clientHeight ) {
			d.pageYOffset = document.documentElement.scrollTop;
			d.pageXOffset = document.documentElement.scrollLeft;
			d.innerHeight = document.documentElement.clientHeight;
			d.innerWidth = document.documentElement.clientWidth;
		} else if( document.body ) {
			d.pageYOffset = document.body.scrollTop;
			d.pageXOffset = document.body.scrollLeft;
			d.innerHeight = document.body.clientHeight;
			d.innerWidth = document.body.clientWidth;
		}
		(evt.pageX) ? x = evt.pageX : x = evt.clientX + d.scrollLeft;
		(evt.pageY) ? y = evt.pageY : x = evt.clientY + d.scrollTop;
		return {x:x, y:y}
	}
});
$.fn.extend({
	bubble:function(options){
		this.each(function () {
	        var distance = 10;
	        var time = 250;
	        var hideDelay = 150;
	        var hideDelayTimer = null;
	        var beingShown = false;
	        var shown = false;
	        var info = $(this).next().css({
	        	position:'absolute',
	        	width: '220px',
	        	border: 'solid 1px #CCC',
	        	background: '#EEE',
	        	display: 'none',
	        	margin: '25px 0 0 0',
	        	padding: '10px',
	        	opacity: '0'
	        });
	        var offset = $(this).offset();
	        $(this).mouseover(function (e) {
	        	// Detect mouse position
				var x = $.coordinate(e).x;
	        	var y = $.coordinate(e).y;
	            if (hideDelayTimer) clearTimeout(hideDelayTimer);
	            if (beingShown || shown) {
	                // don't trigger the animation again
	                return;
	            } else {
	                // reset position of info box
	                beingShown = true;
	                info.css({
	                    top: y,
	                    left: x,
	                    display: 'block'
	                }).animate({
	                    top: '-=' + distance + 'px',
	                    opacity: 1
	                }, time, 'swing', function() {
	                    beingShown = false;
	                    shown = true;
	                });
	            }
	            return false;
	        }).mouseout(function () {
	            if (hideDelayTimer) clearTimeout(hideDelayTimer);
	            hideDelayTimer = setTimeout(function () {
	                hideDelayTimer = null;
	                info.animate({
	                    top: '-=' + distance + 'px',
	                    opacity: 0
	                }, time, 'swing', function () {
	                    shown = false;
	                    info.css('display', 'none');
	                });
	
	            }, hideDelay);
	
	            return false;
	        });
	    });
	},cuDialog: function(o, callback){
		var $dialog = $(this);
		var $replaceCuBlock = (o==undefined||o.replaceCuBlock==undefined||o.replaceCuBlock=="") ? $('#replaceCuBlock') : $('#'+o.replaceCuBlock);
		var entity = (o==undefined||o.entity==undefined||o.entity=="") ? 'Entity' : o.entity;
		var width = (o==undefined||o.width==undefined||o.width=="") ? '' : o.width;
		$dialog.dialog({modal: true, autoOpen: false, width: width});
		var lastDivId;
		var lastHtml;
		$('#create'+entity).click(function(){
			var id = 0;
			if(lastDivId!=undefined)
				$('#'+lastDivId).html(lastHtml);
			lastDivId = 'createDiv';
			lastHtml = $('#'+lastDivId).html();

			$dialog.dialog('open');
			$replaceCuBlock.html($('#createDiv').html());

			$('#'+lastDivId).html('');
			if(callback) callback(id);
		})
		$('.update'+entity).click(function(){
			var id = $(this).attr('id').split('update'+entity)[1];
			if(lastDivId!=undefined)
				$('#'+lastDivId).html(lastHtml);
			lastDivId = 'updateDiv'+id;
			lastHtml = $('#'+lastDivId).html();
			
			$dialog.dialog('open');
			$replaceCuBlock.html($('#updateDiv'+id).html());

			$('#'+lastDivId).html('');
			if(callback) callback(id);
		})
	},showDisplay: function(o, callback){
		var $show = $(this);
		var $replaceShowBlock = (o==undefined||o.replaceShowBlock==undefined) ? $show : $('#'+o.replaceShowBlock);
		var entity = (o==undefined||o.entity==undefined) ? 'Entity' : o.entity;
		var lastDivId;
		var lastHtml;
		$('.show'+entity).mouseover(function(){
			$(this).css('cursor', 'pointer');
		})
		$('.show'+entity).click(function(){
			var id = $(this).attr('id').split('show'+entity)[1];
			if(lastDivId!=undefined){
				$('#'+lastDivId).html(lastHtml);
				$('#'+lastDivId).parent().addClass('rb0');
				$('#'+lastDivId).parent().removeClass('w');
			}
			lastDivId = 'showDiv'+id;
			lastHtml = $('#'+lastDivId).html();

			$show.hide();
			$show.html($('#showDiv'+id).html());
			$show.fadeIn("slow");
			$('#showDiv'+id).parent().removeClass('rb0');
			$('#showDiv'+id).parent().addClass('w');
			if(callback) callback();
		})
		$('.show'+entity+':first').click();
	},sel2sel: function(o, callback){
		var $sel1 = $(this);
		var db = (o==undefined||o.db==undefined||o.db=="") ? [] : eval('(' + o.db + ')');
		var hideEntityName = (o==undefined||o.hideEntityName==undefined) ? '' : o.hideEntityName;
		var hideEntities = (o==undefined||o.hideEntities==undefined||o.hideEntities=="") ? 'hideEntities' : o.hideEntities;
		var $sel2 = $('#'+$sel1.attr('id')+'1');
		var $btn1 = $('#'+$sel1.attr('id')+'btn');
		var $btn2 = $('#'+$sel1.attr('id')+'btn1');
		var generateHtml = function(){
			var html = '';
			$sel2.children('option').each(function(i, n){
				html+='<input type=hidden name='+hideEntityName+'['+i+'].id value='+$(this).val()+' />';
			})
			$('#'+hideEntities).html(html);
		}
		$.each(db, function(i, n){
			$sel1.children('option').each(function(){
				if(n==$(this).val()){
					$sel2.append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
					$(this).remove()
				}
			})
		})
		generateHtml();
		$btn1.click(function(){
			$sel1.children('option:selected').each(function(){
				$sel2.append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
				$(this).remove(); 
			})
			generateHtml();
		})
		$btn2.click(function(){
			$sel2.children('option:selected').each(function(){
				$sel1.append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
				$(this).remove(); 
			})
			generateHtml();
		})
	}
})